package com.dengcj.ecology.common;

import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dengchunjie
 * @Description: 下拉框工具类
 * @date 2022/1/11 17:44
 */
public class SelectBoxUtil {
    /**
     * 根据表名字段名称和下拉选择框值获取下拉选择框描述
     * @param tablename
     * @param columnName
     * @param selectValue
     * @return
     */
    public static String getSelectName(String tablename,int detailIndex, String columnName,String selectValue){
        RecordSet rs = new RecordSet();

        String sql = "select c.selectname from workflow_billfield a " +
                "inner join workflow_bill b on b.id=a.billid " +
                "inner join workflow_selectitem c on c.FIELDID=a.id " +
                "where b.tablename=? and a.fieldname=? and viewtype=? and selectvalue=?";
        int viewtype = 0;
        //如果明细表下标大于-1则代表为明细表
        if(detailIndex > 0){
            viewtype = 1;
            sql += " and detailtable='"+tablename+"_dt"+detailIndex+"'";
        }
        rs.executeQuery(sql, tablename, columnName, viewtype, selectValue);
        if(rs.next()){
            return rs.getString("selectname");
        }

        return "";
    }

    /**
     * 根据字段编号和下拉选择框值获取下拉选择框描述
     * @param fieldid
     * @param selectValue
     * @return
     */
    public static String getSelectName(int fieldid,String selectValue){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select selectname from workflow_selectitem where fieldid=? and selectvalue=?", fieldid, selectValue);
        if(rs.next()){
            return rs.getString("selectname");
        }

        return "";
    }

    /**
     * 根据字段编号和下拉选择框值获取下拉选择框描述
     * @param fieldid
     * @param fieldname
     * @return
     */
    public static int getSelectIndex(int fieldid,String fieldname){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select selectvalue from workflow_selectitem where fieldid=? and selectname=?",fieldid,fieldname);
        if(rs.next()){
            return Util.getIntValue(Util.null2String(rs.getString("selectvalue")));
        } else {
            return -1;
        }
    }

    /**
     * 根据字段编号获取下拉选择框内容
     * @param fieldid
     * @return
     */
    public static List<String> getSelectInfo(int fieldid){
        RecordSet rs = new RecordSet();
        List<String> selectinfoList = new ArrayList<>();
        rs.executeQuery("select selectname from workflow_selectitem where fieldid=? order by selectvalue", fieldid);
        while(rs.next()){
            selectinfoList.add(Util.null2String(rs.getString("selectname")));
        }

        return selectinfoList;
    }

    /**
     * 获取表单的下拉框字段信息
     * @param tablename
     * @return
     */
    public List<Map<String, Object>> getSelectFieldInfo(String tablename){
        RecordSet rs = new RecordSet();
        List<Map<String, Object>> allSelectInfo = new ArrayList<>();
        rs.executeQuery("select a.id,a.fieldname,d.labelname,GROUP_CONCAT(c.SELECTNAME) as selectnames,GROUP_CONCAT(c.SELECTVALUE) as selectindex from workflow_billfield a " +
                "inner join workflow_bill b on b.id=a.billid " +
                "inner join htmllabelinfo d on d.indexid=a.fieldlabel " +
                "right join workflow_selectitem c on c.FIELDID=a.id " +
                "where b.tablename=？ and a.fieldhtmltype=5 and d.languageid=7 GROUP by a.id,a.fieldname,d.labelname", tablename);
        while(rs.next()){
            Map<String, Object> fieldMap = new HashMap<>();
            fieldMap.put("fielid", rs.getString("id"));
            fieldMap.put("fieldname", rs.getString("fieldname"));
            fieldMap.put("labelname", rs.getString("labelname"));

            String[] selectnameArray = rs.getString("selectnames").split(",");
            String[] indexArray = rs.getString("selectindex").split(",");

            Map<String, String> selectInfoMap = new HashMap<>();
            for(int i=0;i<selectnameArray.length;i++){
                selectInfoMap.put(selectnameArray[i],indexArray[i]);
            }

            fieldMap.put("selectinfo", selectInfoMap);

            allSelectInfo.add(fieldMap);
        }

        return allSelectInfo;
    }

    /**
     * 获取表单的下拉框字段信息
     * @param tablename
     * @return
     */
    public Map<String, Object> getSelectFieldInfo(String tablename,String fieldname){
        RecordSet rs = new RecordSet();
        Map<String, Object> fieldMap = null;
        rs.executeQuery("select c.SELECTNAME,c.SELECTVALUE,c.cancel from workflow_billfield a " +
                "inner join workflow_bill b on b.id=a.billid " +
                "inner join htmllabelinfo d on d.indexid=a.fieldlabel " +
                "right join workflow_selectitem c on c.FIELDID=a.id " +
                "where b.tablename=? and a.fieldhtmltype=5 and d.languageid=7 and a.fieldname=?", tablename,fieldname);
        if(rs.next()){
            fieldMap = new HashMap<>();
            fieldMap.put("fielid", rs.getString("id"));
            fieldMap.put("fieldname", rs.getString("fieldname"));
            fieldMap.put("labelname", rs.getString("labelname"));

            String[] selectnameArray = rs.getString("selectnames").split(",");
            String[] indexArray = rs.getString("selectindex").split(",");

            Map<String, String> selectInfoMap = new HashMap<>();
            for(int i=0;i<selectnameArray.length;i++){
                selectInfoMap.put(selectnameArray[i],indexArray[i]);
            }

            fieldMap.put("selectinfo", selectInfoMap);

        }

        return fieldMap;
    }

    /**
     * 验证字段值中的下拉字段
     * @param tableSelectFields
     * @param colMap
     * @return
     */
    public Map<String, Object> validateTableSelecteField(List<Map<String, Object>> tableSelectFields, Map<String, Object> colMap){
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, String>> errorColList = new ArrayList<>();
        int listSize = tableSelectFields.size();
        for(int i=0;i<listSize;i++){
            Map<String, Object> selectField = tableSelectFields.get(i);
            String fieldname = selectField.get("fieldname").toString();
            if(colMap.containsKey(fieldname)){
                Map<String, String> selectMap = (Map<String, String>) selectField.get("selectinfo");
                String colValue = colMap.get(fieldname).toString();
                if(!selectMap.containsKey(colValue)){
                    Map<String, String> errorColMap = new HashMap<>();
                    errorColMap.put("field_value", colValue);
                    errorColMap.put("field_label", selectField.get("labelname").toString());

                    errorColList.add(errorColMap);
                } else {
                    //如果匹配到选项文本则替换对应文本为index
                    colMap.put(fieldname,selectMap.get(colValue));
                }
            }
        }

        resultMap.put("error_cols",errorColList);
        resultMap.put("col_map",colMap);

        return resultMap;
    }
}
